package com.cloud.service;

import java.util.ArrayList;
import java.util.Date;
import com.alibaba.fastjson.JSONObject;
import com.cloud.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.cloud.bean.User;
import com.cloud.mapper.UserManagerMapper;
import com.cloud.mapper.UserMapper;
import javax.annotation.Resource;

@Service
public class UserService {
	
	private static Logger log = LoggerFactory.getLogger(UserService.class);
	
	@Resource
	private UserManagerMapper userManagerMapper;

	@Resource
	private UserMapper userMapper;
	
	public R queryAllUser(JSONObject params) {
		Integer num;
		PageUtil<User> pageUtil;
		ArrayList<User> list;
		if (params.getBoolean("isAdmin")){
			num = userMapper.selectCount();
			pageUtil = new PageUtil<User>(params.getInteger("page"),params.getInteger("size"),num);
			list = userMapper.selectByPage(pageUtil.getStart(),pageUtil.getEnd());
		}else{
			Integer id = params.getInteger("id");
			num = userMapper.selectUserCount(id);
			pageUtil = new PageUtil<User>(params.getInteger("page"),params.getInteger("size"),num);
			list = userMapper.selectUserByPage(id,pageUtil.getStart(),pageUtil.getEnd());
		}
		pageUtil.setList(list);
		return R.ok(pageUtil);
	}

	public R updateUserInfo(JSONObject params) {
		log.info("更新用户信息请求参数 "+params);
		Integer result = 9;
		Boolean isAdmin = params.getBoolean("isAdmin");
		Integer status = params.getInteger("status");
		String password = params.getString("password");
		if (password != null) {
			String pwd = password;
			try {
				pwd = SecUtil.MD5(password);
			}catch (Exception e){
				log.error(e.getMessage());
				e.printStackTrace();
			}
			params.put("password",pwd);
		}
		params.put("updatetime",DateUtils.date1String(new Date()));
		if (!isAdmin){
			params.put("balance",null);
			params.put("agentlevel",null);
			params.put("viplevel",null);
			params.put("status",null);
			params.put("loginip",null);
			params.put("invitationcode",null);
			params.put("pid",null);
		}
		if (status != null) {
			if (status == 1) {
				log.info("冻结用户: {}", status);
				//冻结本人
				userMapper.updateOne(params);
			} else if (status == 2) {
				log.info("冻结用户及下级: {}", status);
				//冻结本人及下级
				params.put("status",1);
				result = userMapper.updateOne(params);
				result = userMapper.updateMany(params);
			} else {
				result = userMapper.updateOne(params);
			}
		}else {
			result = userMapper.updateOne(params);
		}
		return R.ok(result);
	}

	public R deleteUser(JSONObject params) {
		userManagerMapper.deleteUserById(params.getInteger("id"));
		return R.ok();
	}

	/**
	 * 查询单个用户信息
	 * @param params
	 * @return
	 */
	public R queryUser(JSONObject params) {
		if(params.getBoolean("isAdmin")){
			Integer id = params.getInteger("id");
			User user = userMapper.selectById(id);
			return R.ok(user);
		}else{
			Integer id = params.getInteger("id");
			User user = userMapper.selectById(id);
			return R.ok(user);
		}
	}
}
